package com.tensuqare.user.service;

import com.tensuqare.user.dao.UserDao;
import com.tensuqare.user.pojo.User;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    //发送短信息
    public void sendMessage(String mobile) {
        //生成随机验证码
        String code = (long)(Math.random() * 1000000) + "";
        //定义一个map转发数据
        Map map = new HashMap();
        map.put("code", code);
        map.put("mobile", mobile);
        System.out.println("map = " + map);
        //使用redis将其数据保存起来，为以后验证使用，数据在redis中的时间为5分钟
        redisTemplate.opsForValue().set(mobile, code, 5, TimeUnit.MINUTES);
        //使用rabbitTemplate将消息发送出去
        rabbitTemplate.convertAndSend("zelin", map);
    }

    //注册用户
    public void register(User user ,String code) {
        //从redis中获取code
        String code1 = (String) redisTemplate.opsForValue().get(user.getMobile());
        //如果验证不存在说明过期了
        if(code1==null){
            throw new RuntimeException("请点击获取短信验证码");
        }
        if(!code.equals(code1)){
            throw new RuntimeException("验证码输入不正确");
        }
        //注册用户
        user.setId(idWorker.nextId()+"");
        user.setFollowcount(0);//关注数
        user.setFanscount(0);//粉丝数
        user.setOnline(0L);//在线时长
        user.setRegdate(new Date());//注册日期
        user.setUpdatedate(new Date());//更新日期
        user.setLastdate(new Date());//最后登陆日期
        userDao.save(user);
    }
    //登录
    public User login(User user) {
        //根据user的mobile访问数据库获取对应的user信息
        User user1 = userDao.findByMobile(user.getMobile());
        if(user1!=null&&bCryptPasswordEncoder.matches(user.getPassword(),user1.getPassword())){
            return user1;
        }
        return null;
    }

    //增加用户
    public void addUser(User user) {
        System.out.println("user = " + user);
        user.setId(idWorker.nextId()+"");
        //使用bcry加密密码
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        //设置粉丝数
        user.setFanscount(0);
        //设置关注数
        user.setFollowcount(0);
        userDao.save(user);
    }
    //根据ID删除
    public void deleteUser(String userId) {
        userDao.deleteById(userId);
    }
    //增加关注数
    public void addfollow(String userid, int x) {
        userDao.addfollow(userid,x);
    }
    //增加粉丝数
    public void addfans(String userid, int x) {
        userDao.addfans(userid,x);
    }
}
